// pages/scrollviewrefresh2/scrollviewrefresh2.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hideHeader: true,
    hideBottom: true,
    upperThreshold: -60,
    lowerThreshold: -60,
    categoryList: null,
    currentCategoryIndex: 0,
    loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var data = [
      [
        { id: "10000", name: '分类0 - 商品0', background: 'lightblue' },
        { id: "10001", name: '分类0 - 商品1', background: 'lightcoral' },
        { id: "10002", name: '分类0 - 商品2', background: 'lightgoldenrodyellow' },
        { id: "10003", name: '分类0 - 商品3', background: 'lightgreen' },
        { id: "10004", name: '分类0 - 商品4', background: 'lightgray' },
        { id: "10005", name: '分类0 - 商品5', background: 'lightslategray' } ,
        { id: "10006", name: '分类0 - 商品6', background: 'lightblue' },
      ],
      [
        { id: "20000", name: '分类1 - 商品0', background: 'lightgreen' },
        { id: "20001", name: '分类1 - 商品1', background: 'lightcoral' },
        { id: "20002", name: '分类1 - 商品2', background: 'lightslategray' },
        { id: "20003", name: '分类1 - 商品3', background: 'lightblue'},
        { id: "20004", name: '分类1 - 商品4', background: 'lightcoral' },
        { id: "20005", name: '分类1 - 商品5', background: 'lightgray' },
        { id: "20006", name: '分类1 - 商品6', background: 'lightgoldenrodyellow'},
      ],
      [
        { id: "30000", name: '分类2 - 商品0', background: 'lightgreen' },
        {
          id: "30001", name: '分类2 - 商品1', background: 'lightcoral' },
        { id: "30002", name: '分类2 - 商品2', background: 'lightslategray' },
        { id: "30003", name: '分类2 - 商品3', background: 'lightblue' },
        { id: "30004", name: '分类2 - 商品4', background: 'lightcoral'},
        { id: "30005", name: '分类2 - 商品5', background: 'lightgoldenrodyellow' },
        { id: "30006", name: '分类2 - 商品6', background: 'lightgreen'},
      ],
    ]

    this.setData({
      categoryList: data
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 下拉刷新
  refresh: function (e) {
    if (this.data.currentCategoryIndex == 0 || this.data.loading)
      return;
    console.log('****** refresh:下拉刷新 *******')
    this.data.loading  = true
    var self = this
    setTimeout(function () {
      console.log('下拉刷新数据');
      wx.vibrateShort()
      self.setData({
        hideHeader: false
      })
      self.getData(1);
    }, 300);
  },

  // 上拉加载更多
  loadMore: function () {
    if (this.data.currentCategoryIndex == this.data.categoryList.length - 1 || this.data.loading)
      return;

    console.log('****** loadMore:上拉加载更多 *******')
    this.data.loading  = true
    var self = this;
    setTimeout(function () {
      console.log('加载更多数据');
      wx.vibrateShort()
      self.setData({
        hideBottom: false
      })
      self.getData(2);
    }, 300);
  },

  // 获取数据 - flag:1 下拉刷新；  flag:2 加载更多；
  getData: function (flag) {
    var that = this
    if (flag == 1) { // 下拉刷新
      setTimeout(function () {
        that.setData({
          hideHeader: true,
          currentCategoryIndex: (that.data.currentCategoryIndex > 0 ? that.data.currentCategoryIndex-1 : 0)
        })
        that.data.loading  = false
        wx.vibrateShort()
      }, 2000);
    } else if (flag == 2) { // 加载更多
      setTimeout(function () {
        that.setData({
          hideBottom: true,
          currentCategoryIndex: (that.data.currentCategoryIndex < that.data.categoryList.length - 1 ? that.data.currentCategoryIndex + 1 : that.data.categoryList.length - 1)
        })

        that.setData({
          toView: "item0",
        })
        that.data.loading = false
        wx.vibrateShort()
      }, 2000);
    }
  },
})